//index.js
//获取应用实例
import{getUrl}from "../../utils/url";
const app = getApp()
let _this;
Page({
  data: {
    price: '',
    des: '',
    phone: '',
    tags:'',
    address:{}
  },
  navTo(e) {
    app.com.navTo(e)
  },
  formSubmit(e) {
    let formId = e.detail.formId
    if (e.detail.value.des == '') {
      wx.showToast({
        title: '请填写帮助内容',
        icon: 'none'
      })
    } else if (e.detail.value.price == '') {
      wx.showToast({
        title: '请输入赏金',
        icon: 'none'
      })
    } else if (this.data.address == ''){
      wx.showToast({
        title: '请选择目的地',
        icon: 'none'
      })
    } else if (e.detail.value.price <1){
      wx.showToast({
        title: '输入金额不能小于1',
        icon: 'none'
      })
      return;
    }/* else {
      wx.showLoading({
        title: '加载中',
      })
    } */
    else{
   let orders={
      title:e.detail.value.des,//帮助内容
      money:e.detail.value.price,//悬赏金额
      addressId:this.data.address[0].id,//目的地id
      userId:wx.getStorageSync('user').id,//发布人的id
      schoolId:wx.getStorageSync('area').id//学校id
   }
   wx.request({
    url: getUrl()+`/orderS/save`,  //路径
    data:orders,//参数
    success: function(res) {
      console.log(res.data);
      if(res.data!=null&&res.data!=''){
         wx.showToast({
           title: '发布成功!',
         }),
         setTimeout(function () {
              wx.switchTab({
          url: '../../pages/banzu/banzu',
        })
          //要延时执行的代码
         }, 2000) //延迟时间 这里是1秒
      }else{
       wx.showToast({
         title: '余额不足,请充值',
         icon:'none'
       })
           
      }
    }
    
  })
        
}


  },
  wxpay(msg) {
    app.com.wxpay(msg)
  },
  navTo(e) {
    app.com.navTo(e)
  },
  navToArea() {
    wx.navigateTo({
      url: '/pages/area/area',
    })
  },
  publist() {
    wx.navigateTo({
      url: '/pages/pub/pub',
    })
  },
  tagsClick(e){
    let index = e.currentTarget.dataset.index
    this.setData({
      price:this.data.msg.tagsFilter[index].price,
      des: this.data.msg.tagsFilter[index].label
    })
  },
 
  onShow() {
    // if (wx.getStorageSync('area')) {
    //   this.setData({
    //     area: wx.getStorageSync('area')
    //   })
    // } else {
    //   wx.navigateTo({
    //     url: '/pages/area/area',
    //   })
    // }
    this.setData({
      address:wx.getStorageSync('address')
    })
  }

})
